<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Alumni extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("usraccount_model", "uam");
        $this->load->model("usrprofile_model", "upm");
    }

    function index() {
        $perpage = 20;
        $curpage = $this->uri->segment(3);
        $data["txtSearch"] = $this->input->post("txtSearch");
        $data["ddAngkatan"] = $this->input->post("ddAngkatan");
        $where = "usr_profile.mpa_id_alumni = usr_profile.mpa_id_alumni ";
        if ($data["txtSearch"] != "")
            $where .= " and (usr_profile.up_fullname like '%" . addslashes($data["txtSearch"]) . "%' or usr_profile.up_email like '%" . addslashes($data["txtSearch"]) . "%')";
        if ($data["ddAngkatan"] != "")
            $where .= " and mpa_graduate_year = '" . addslashes($data["ddAngkatan"]) . "'";
        $data["alumni"] = $this->upm->getAllProfile($curpage, $perpage, @$where, @$like, "usr_profile.up_fullname");
        $this->popuppaging(base_url()."alumni/index", $this->upm->getAllProfileCount(@$where, @$like), $perpage);
        $this->myview(@$data, "alumni/index");
    }

    function rpassword() {
        if ($this->sess->isadmin == 1) {
            $this->session->set_flashdata("message", "Password telah dikirim ke email user");
            $this->session->set_flashdata("redir", base_url() . "alumni");
            redirect(base_url() . "alert/success");
        } else {
            $this->session->set_flashdata("message", "Anda tidak mempunyai hak akses untuk halaman ini.");
            $this->session->set_flashdata("redir", base_url());
            redirect(base_url() . "alert/warning");
        }
    }

    function lock() {
        if ($this->sess->isadmin == 1) {
            $this->uam->update($this->uri->segment(3), array("locked" => ($this->uri->segment(4)=="1"?"0":"1")));
            $this->session->set_flashdata("message", "User berhasil di ". ($this->uri->segment(4) == "1" ? "Unlock" : "Lock" ) .".");
            $this->session->set_flashdata("redir", base_url() . "alumni");
            redirect(base_url() . "alert/success");
        } else {
            $this->session->set_flashdata("message", "Anda tidak mempunyai hak akses untuk halaman ini.");
            $this->session->set_flashdata("redir", base_url());
            redirect(base_url() . "alert/warning");
        }
    }

    function admin() {
        if ($this->sess->isadmin == 1) {
            $this->uam->update($this->uri->segment(3), array("isadmin" => ($this->uri->segment(4)=="1"?"0":"1")));
            $this->session->set_flashdata("message", "User berhasil ". ($this->uri->segment(4) == "1" ? "dihilangkan hak aksesnya sebagai Admin" : "diberikan hak akses sebagai Admin" ) .".");
            $this->session->set_flashdata("redir", base_url() . "alumni");
            redirect(base_url() . "alert/success");
        } else {
            $this->session->set_flashdata("message", "Anda tidak mempunyai hak akses untuk halaman ini.");
            $this->session->set_flashdata("redir", base_url());
            redirect(base_url() . "alert/warning");
        }
    }

    function edit() {
        if ($this->input->post()) {
            $data["alumni"]->alumninama = $this->input->post("txtNama");
            $data["alumni"]->alumniangkatan = $this->input->post("txtAngkatan");
            $data["alumni"]->alumnialamatasal = $this->input->post("txtAlamatasal");
            $data["alumni"]->alumnikotaasal = $this->input->post("txtKotaasal");
            $data["alumni"]->alumnipropinsiasal = $this->input->post("txtPropinsiasal");
            $data["alumni"]->alumnialamatdomisili = $this->input->post("txtAlamatdomisili");
            $data["alumni"]->alumnikotadomisili = $this->input->post("txtKotadomisili");
            $data["alumni"]->alumnipropinsidomisili = $this->input->post("txtPropinsidomisili");
            $data["alumni"]->alumnicontact = $this->input->post("txtContact");
            $data["alumni"]->alumniemail = $this->input->post("txtEmail");
            $data["alumni"]->alumnijenkel = $this->input->post("ddJenkel");
            $data["alumni"]->alumnistatus = $this->input->post("txtStatus");
            $data["alumni"]->alumnikeahlian = $this->input->post("txtKeahlian");

            $data["alumni"]->alumninama == "" ? $data["err"]->alumninama = strErr("mustfilled") : "";
            $data["alumni"]->alumniangkatan == "" ? $data["err"]->alumniangkatan = strErr("mustchoose") : "";
            $data["alumni"]->alumnialamatasal == "" ? $data["err"]->alumnialamatasal = strErr("mustfilled") : "";
            $data["alumni"]->alumnikotaasal == "" ? $data["err"]->alumnikotaasal = strErr("mustchoose") : "";
            $data["alumni"]->alumnipropinsiasal == "" ? $data["err"]->alumnipropinsiasal = strErr("mustchoose") : "";
            $data["alumni"]->alumnialamatdomisili == "" ? $data["err"]->alumnialamatdomisili = strErr("mustfilled") : "";
            $data["alumni"]->alumnikotadomisili == "" ? $data["err"]->alumnikotadomisili = strErr("mustchoose") : "";
            $data["alumni"]->alumnipropinsidomisili == "" ? $data["err"]->alumnipropinsidomisili = strErr("mustchoose") : "";
            $data["alumni"]->alumnicontact == "" ? $data["err"]->alumnicontact = strErr("mustfilled") : "";
            $data["alumni"]->alumniemail == "" ? $data["err"]->alumniemail = strErr("mustfilled") : "";
            !validEmail($data["alumni"]->alumniemail) ? $data["err"]->alumniemail = strErr("mustfilled") : "";
            $data["alumni"]->alumnistatus == "" ? $data["err"]->alumnistatus = strErr("mustfilled") : "";

            if (@$data["err"]) {
                $this->myview(@$data, "alumni/edit", false, 1);
            } else {
                if ($this->am->update($this->uri->segment(3), $data["alumni"])) {
                    $this->session->set_flashdata("message", "Alumni berhasil diubah");
                    $this->session->set_flashdata("redir", base_url() . "alumni");
                    redirect(base_url() . "alert/success");
                } else {
                    $this->session->set_flashdata("message", "Alumni gagal diubah");
                    $this->session->set_flashdata("redir", "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
                    redirect(base_url() . "alert/error");
                }
            }
        } else {
            $data["alumni"] = $this->am->getById($this->uri->segment(3));
            $this->myview(@$data, "alumni/edit", false, 1);
        }
    }

    function login() {
        $dt = $this->uam->login($this->input->post("txtUname"), $this->input->post("txtUpass"));
        if ($dt) {
            $this->session->set_userdata($dt);
            if ($dt->isdelete == "0") {
                $this->session->keep_flashdata("redir");
                redirect(base_url() . "profile");
            } else {
                redirect(@$this->session->flashdata("redir") == "" ? base_url() : @$this->session->flashdata("redir"));
            }
        } else {
            $this->session->keep_flashdata("redir");
            $data["err"] = $this->input->post() ? strErr("wrongpass") : "";
            $this->myview($data, "alumni/login", true);
        }
    }

    function fpass() {
        if ($this->input->post()) {
            $this->session->set_flashdata("message", "Password baru dikirim ke alamat email Anda.");
            $this->session->set_flashdata("redir", base_url());
            redirect(base_url() . "alert/success");
        } else {
            $this->myview(@$data, 'alumni/fpass', true);
        }
    }

    function logout() {
        $this->session->sess_destroy();
        redirect(base_url());
    }

    function profile() {
        $data["alumni"] = $this->upm->getById(@$this->sess->userid);
    }

    function adduser() {
        $file = @$_FILES['txtAlumni'];
        //pre($file);
        if ($file) {
            $this->load->library('csvreader');
            $csvData = $this->csvreader->parse_file($file["tmp_name"]);
            $success = 0;
            $error = 0;
            $noerror="";
            $total = 0;
            foreach ($csvData as $k => $v) {
                //pre($v);
                //no,usrname,email,nis,gender,graduateyear,phone,status
                $iId = $v["no"];
                $username = $v["usrname"];
                $accountemail = $v["email"];
                $password = md5($accountemail);
                $nis = $v["nis"];
                $gender = $v["gender"];
                $graduateyear = $v["graduateyear"];
                $phone = $v["phone"];
                $status = $v["status"];

                $twitterid = "";
                $facebookid = "";
                $openid = "";
                $gmailid = "";

                $pob = "";
                $dob = "";
                $homeaddress = "";
                $homecityid = "";
                $homeprovinceid = "";
                $homezipcode = "";
                $hometownaddress = "";
                $hometowncityid = "";
                $hometownzipcode = "";
                $hometownprovinceid = "";
                $picture = "";

                $code = 1;
                if ($username != "") {
                    if (!validEmail($accountemail)) {
                        $code = 0;
                        $error++;
                        $noerror[$total] = $iId;
                        $reason[$total] = "Email tidak valid";
                    } else {
                        $eksis = $this->uam->getAllCount(array("accountemail"=>$accountemail));
                        if ($eksis == 0) {
                            if ($username == "") {
                                $username = "Default";
                            }
                            if ($nis == "") {
                                $nis = "";
                            }
                            if ($graduateyear == "") {
                                $graduateyear = "";
                            }
                            if ($gender != "L" || $gender != "P") {
                                $gender = "L";
                            }
                            if ($phone == "") {
                                $phone = "";
                            }
                            if ($status == "") {
                                $status = "s";
                            }
                            $success++;
                        } else {
                            $code = 0;
                            $error++;
                            $noerror[$total] = $iId;
                            $reason[$total] = "Email sudah didaftarkan";
                        }
                    }
                } else {
                    $error++;
                    $noerror[$total] = $iId;
                    $reason[$total] = "Username kosong";
                    //$noerror .= $iId.";";
                }
                $total++;
                if ($code == 1) {
                    $userid = $this->uam->insert($username, $password, $accountemail, $twitterid, $facebookid, $openid, $gmailid, 0, 0);
                    if ($userid != "") {
                        $this->upm->insert($userid, $nis, $username, $pob, $dob, $gender, $graduateyear, $phone, $accountemail, $homeaddress, $homecityid, $homeprovinceid, $homezipcode, $hometownaddress, $hometowncityid, $hometownprovinceid, $hometownzipcode, $status, $picture);
                        $this->uam->insert_que_pas($username, $accountemail, $accountemail);
                    }
                }
            }
            if ($error == 0) {
                $noerror[0] = "-";
            }
            $ret->total = $total;
            $ret->error = $error;
            $ret->success = $success;
            $ret->noerror = $noerror;
            $ret->reason = $reason;
            header('Content-Type: application/json', true);
            echo json_encode($ret);
        } else {
            $this->mypopup("Upload User", @$data, "alumni/adduser");
        }
    }

}